import { BaseApiService } from '../base/api.service';
import type { Project } from '@/types/business/project';
import type { PaginatedResponse } from '@/types/common/api';
import type { RepositorySummary } from '@/types/business/project';
import type { ProjectSummary } from '@/types/business/project';
import type { MonthlyStat } from '@/types/business/project';
import type { AuthorStats } from '@/types/business/project';

/**
 * 项目服务类
 */
export class ProjectService extends BaseApiService {
  constructor() {
    super('/api/projects');
  }

  /**
   * 获取项目列表
   */
  async getProjects(page: number = 1, size: number = 10): Promise<PaginatedResponse<Project>> {
    const params = { page, size };
    return this.getPaginated<Project>('', params);
  }

  /**
   * 添加项目
   */
  async addProject(project: Omit<Project, 'id' | 'createdAt' | 'updatedAt'>): Promise<Project> {
    return this.post<Project>('', project);
  }

  /**
   * 更新项目
   */
  async updateProject(id: string, project: Partial<Project>): Promise<Project> {
    return this.put<Project>(`${id}`, project);
  }

  /**
   * 删除项目
   */
  async deleteProject(id: string): Promise<void> {
    return this.delete<void>(`${id}`);
  }

  async getRepositoryNames(projectId: string): Promise<RepositorySummary[]> {
    return this.get<RepositorySummary[]>(`${projectId}/repositories`);
  }

  async addRepository(projectId: string, repositoryId: string): Promise<void> {
    return this.post<void>(`${projectId}/repositories`, { repositoryId });
  }

  async deleteRepository(projectRepoId: string): Promise<void> {
    return this.delete<void>(`repositories/${projectRepoId}`);
  }

  async getProjectSummaries(
    params: { page?: number; size?: number; name?: string; sortBy?: string; sortDir?: string } = {},
  ): Promise<PaginatedResponse<ProjectSummary>> {
    return this.getPaginated<ProjectSummary>('summaries', params);
  }

  async getProjectMonthlyStats(projectId: string): Promise<MonthlyStat[]> {
    return this.get<MonthlyStat[]>(`${projectId}/monthly-stats`);
  }

  async getProjectAuthorStats(projectId: string): Promise<AuthorStats[]> {
    return this.get<AuthorStats[]>(`${projectId}/author-stats`);
  }
}

// 导出单例实例
export const projectService = new ProjectService();
